package com.ffyc.cms.controller;
import com.ffyc.cms.common.CommonResult;
import com.ffyc.cms.model.Admin;
import com.ffyc.cms.model.Advertising;
import com.ffyc.cms.model.Friendlink;
import com.ffyc.cms.service.AdvertisingService;
import com.ffyc.cms.service.FriendlinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 链接显示控制层
 */
@RestController
@RequestMapping(path = "friendlink")
public class FriendlinkController {
    @Autowired
    FriendlinkService friendlinkService;
    @RequestMapping(value = "getfriendlinklist")
    public CommonResult getfriendlinklist(){
        CommonResult commonResult=null;
        try {
            List<Friendlink> friendlinkList=friendlinkService.getfriendlinklist();
            if (friendlinkList==null){
                commonResult=new CommonResult(201,"友情链接查询为空",null);
            }else {
                commonResult=new CommonResult(200,"友情链接查询成功",friendlinkList);
            }
        }catch (Exception e){
            e.printStackTrace();
            commonResult=new CommonResult(500,"友情链接查询失败",null);
        }
        return commonResult;
    }

    /**
     * 通过id获取友情链接
     * @param id
     * @return
     */
    @RequestMapping(value = "getLinkById")
    public CommonResult getLinkById(Integer id){
        CommonResult commonResult=null;
        try {
            Friendlink friendlink =friendlinkService.getLinkById(id);
            commonResult=new CommonResult(200,"查找成功",friendlink);

        }catch (Exception e){
            e.printStackTrace();
            commonResult=new CommonResult(500,"友情链接查询失败",null);
        }
        return commonResult;
    }

    /**
     * 查找是否已存在友情链接名
     * @param linkname
     * @return
     */
    @RequestMapping(value = "findLinkName")
    public CommonResult findLinkName(String linkname){
        CommonResult commonResult;
        try {
            boolean flag =friendlinkService.findLinkName(linkname);
            if (flag){
                commonResult=new CommonResult(200,"友情链接名合法",null);
            }else {
                commonResult=new CommonResult(201,"友情链接名已存在",null);
            }
        }catch (Exception e){
            e.printStackTrace();
            commonResult=new CommonResult(500,"友情链接查询失败",null);
        }
        return commonResult;
    }

    /**
     * 保存新建的友情链接
     * @param session
     * @param friendlink
     * @return
     */
    @RequestMapping(value = "savefriendlink")
    public CommonResult savefriendlink(HttpSession session,Friendlink friendlink){
        CommonResult commonResult;
        try {
            Admin admin = (Admin)session.getAttribute("admin");
            friendlink.setAdmin(admin);
            friendlinkService.savefriendlink(friendlink);

            commonResult=new CommonResult(200,"友情链接保存成功",null);

        }catch (Exception e){
            e.printStackTrace();
            commonResult=new CommonResult(500,"友情链接查询失败",null);
        }
        return commonResult;
    }

    /**
     * 修改友情链接
     * @param session
     * @param friendlink
     * @return
     */
    @RequestMapping(value = "updatefriendlink")
    public CommonResult updatefriendlink(HttpSession session,Friendlink friendlink){
        CommonResult commonResult;
        try {
            Admin admin = (Admin)session.getAttribute("admin");
            friendlink.setAdmin(admin);
            friendlinkService.updatefriendlink(friendlink);

            commonResult=new CommonResult(200,"友情链接修改成功",null);

        }catch (Exception e){
            e.printStackTrace();
            commonResult=new CommonResult(500,"友情链接修改失败",null);
        }
        return commonResult;
    }

}
